/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.com.view;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.com.controller.BaseCliente;
import org.com.model.Cliente;

/**
 *
 * @author Afonso Rodrigues
 */
public class FluxoServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");


        String direcao = request.getParameter("direcao");
        
        
        HttpSession session = request.getSession();
        switch (direcao) {
            case"voltar":
               response.sendRedirect(GeneralConstant.opcoes); 
                break;
                
            case "logar":
                logar(request, response, session);
                break;
            case "produto":
                response.sendRedirect(GeneralConstant.manutencaoProd);
                break;
            case "cliente":
                response.sendRedirect(GeneralConstant.cliente);
                break;
        }






        try (PrintWriter out = response.getWriter()) {
            /*
             * TODO output your page here. You may use following sample code.
             */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet FluxoServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet FluxoServlet at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private void logar(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
        String login = request.getParameter("login");
        String pwd = request.getParameter("password");

        if ("admin".equals(login)) {
            if ("admin".equals(pwd)) {
                session.setAttribute("usuarioLogado", "Administrador");
                response.sendRedirect(GeneralConstant.opcoes);
            } else {
                session.setAttribute("login", "invalido");
                response.sendRedirect(GeneralConstant.principal);
            }
        } else if ("admin1".equals(login)) {
            if ("admin1".equals(pwd)) {
                session.setAttribute("usuarioLogado", "Administrador");
                response.sendRedirect(GeneralConstant.opcoes);
            } else {
                session.setAttribute("login", "invalido");
                response.sendRedirect(GeneralConstant.principal);
            }
        } else if ("admin2".equals(login)) {
            if ("admin2".equals(pwd)) {
                session.setAttribute("usuarioLogado", "Administrador");
                response.sendRedirect(GeneralConstant.opcoes);
            } else {
                session.setAttribute("login", "invalido");
                response.sendRedirect(GeneralConstant.principal);
            }
        } else if (verificaLogin(login, pwd)) {
            session.setAttribute("usuarioLogado", clienteLogado(login, pwd));
            response.sendRedirect(GeneralConstant.exibirProdutos);
        } else {
            session.setAttribute("login", "invalido");
            response.sendRedirect(GeneralConstant.principal);
        }
    }

    private boolean verificaLogin(String login, String pwd) {
        boolean retorno = false;
        BaseCliente bC = new BaseCliente();
        ArrayList<Cliente> clientes = (ArrayList<Cliente>) bC.getClientes();

        for (Cliente cliente : clientes) {
            if (cliente.getLogin().equals(login)) {
                if (cliente.getPassword().equals(pwd)) {
                    return true;
                }
            }
        }

        return retorno;
    }

    private Object clienteLogado(String login, String pwd) {
           Cliente c1 = null;    
        BaseCliente bC = new BaseCliente();
        ArrayList<Cliente> clientes = (ArrayList<Cliente>) bC.getClientes();

        for (Cliente cliente : clientes) {
            if (cliente.getLogin().equals(login)) {
                if (cliente.getPassword().equals(pwd)) {
                 c1 = cliente;
                 return c1;
                }
            }
        }

        return c1;
    }
}
